package cn.b.sky.shop.storage;

import cn.b.sky.shop.storage.exception.StorageException;
import cn.b.sky.shop.storage.service.CloudStorageClient;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @Author: lidapeng
 * @Description:
 * @Date Created in 8/17/23 4:48 PM
 * @Modified By: lidapeng
 * Copyright(c) dbappsecurity.com.cn
 */
public class StorageManager {

    private final static CopyOnWriteArrayList<StorageInfo> registeredStorages = new CopyOnWriteArrayList<>();

    static {
        loadInitialStorage();
    }


    private static void loadInitialStorage() {

    }

    public static synchronized void registerStorage(CloudStorageClient client)
            throws StorageException {

        registerStorage(client, null);
    }

    public static synchronized void registerStorage(CloudStorageClient client,
                                                    String clientType)
            throws StorageException {

        /* Register the driver if it has not already been added to our list */
        if (client != null) {
            registeredStorages.addIfAbsent(new StorageInfo(client, clientType));
        } else {
            // This is for compatibility with the original DriverManager
            throw new NullPointerException();
        }

    }
}


class StorageInfo {
    final CloudStorageClient client;
    String clientType;

    StorageInfo(CloudStorageClient client, String clientType) {
        this.client = client;
        this.clientType = clientType;
    }

    @Override
    public boolean equals(Object other) {
        return (other instanceof StorageInfo)
                && this.client == ((StorageInfo) other).client;
    }

    @Override
    public int hashCode() {
        return client.hashCode();
    }
}